using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Zune.Forms
{
    /// <summary>
    /// A control used in the Keyboard control.
    /// </summary>
    internal class KeyboardKey : SysControl
    {
        #region Private/Internal Members
        internal delegate void KeyboardEventHandler(object sender, Keys key);
        internal event KeyboardEventHandler KeyDown;
        internal event KeyboardEventHandler KeyUp;
        internal bool _isPressed = false;
        internal Keys _key = Keys.A;
        internal Texture2D _background;
        internal Texture2D _pressedBackground;
        internal string _keyText;
        internal Keyboard _keyboard;
        internal Vector2 _pressedLocation;
        internal Vector2 _pressedSize;
        internal Color _pressedForColor = Color.Black;
        internal Texture2D _vLine;
        internal Texture2D _hLine;
        internal Texture2D _hPressedLine;
        internal Texture2D _vPressedLine;
        #endregion

        #region Constructors
        internal KeyboardKey(Keyboard keyboard) : base(keyboard)
        {
            _backColor = Color.Black;
            _foreColor = Color.White;
            _size = new Vector2(26, 40);
            _location = new Vector2(0, 0);
            _parent = keyboard;
            _keyboard = keyboard;
            _pressedLocation = new Vector2(0, 0);
            _pressedSize = new Vector2(26, 75);
        }
        #endregion

        #region Overrides
        internal override void LoadContent()
        {
            base.LoadContent();
        }

        internal override void Draw(GameTime gameTime)
        {
            string text = _keyText.ToLower();
            if (_keyboard._shiftClicked)
                text = _keyText.ToUpper();

            if (_isPressed)
            {
                _pressedLocation = new Vector2(_location.X - ((_pressedBackground.Width - _background.Width) / 2), _location.Y - 35);
                _pressedSize = new Vector2(_pressedBackground.Width, _pressedBackground.Height);
            
                base.SpriteBatch.Draw(_pressedBackground, _pressedLocation, Color.White);
                if (_key != Keys.Enter)
                {
                    Vector2 textPos = Utilities.CenterHorizontalTextVector2(_keyboard._fontDown, text, _pressedLocation, _pressedSize);
                    base.SpriteBatch.DrawString(_keyboard._fontDown, text, textPos, _pressedForColor);
                }
            }
            else
            {
                _size = new Vector2(_background.Width, _background.Height);

                base.SpriteBatch.Draw(_background, _location, Color.White);
                Drawing.DrawBorder(base.SpriteBatch, _hLine, _vLine, _location, _size);
                if (_key != Keys.Enter)
                {
                    Vector2 textPos = Utilities.CenterTextVector2(_keyboard._font, text, _location, _size);
                    base.SpriteBatch.DrawString(_keyboard._font, text, textPos, _foreColor);
                }
            }

            //base.Draw(gameTime);
        }

        internal override void Update(GameTime gameTime, TouchCollection touches)
        {
            if (touches.Count > 0)
            {
                TouchLocation touch = touches[0];
                switch (touch.State)
                {
                    case TouchLocationState.Pressed:
                        if (Utilities.IsControlTouched(touch.Position, _location, new Vector2(_background.Width, _background.Height)))
                        {
                            _isPressed = true;

                            if (KeyDown != null)
                            {
                                KeyDown(this, _key);
                            }
                        }
                        break;
                    case TouchLocationState.Moved:
                        if (!Utilities.IsControlInsideTouched(touch.Position, _location, new Vector2(_background.Width, _background.Height), 1))
                        {
                            _isPressed = false;
                        }
                        else if (Utilities.IsControlInsideTouched(touch.Position, _location, new Vector2(_background.Width, _background.Height), 1))
                        {
                            if (!_isPressed)
                            {
                                _isPressed = true;

                                if (KeyDown != null)
                                {
                                    KeyDown(this, _key);
                                }
                            }
                        }
                        break;
                    case TouchLocationState.Released:
                        if (Utilities.IsControlInsideTouched(touch.Position, _location, new Vector2(_background.Width, _background.Height), 1))
                        {
                            if (KeyUp != null)
                            {
                                KeyUp(this, _key);
                            }
                        }
                        _isPressed = false;
                        break;
                }
            }

            base.Update(gameTime, touches);
        }
        #endregion

        #region Internal Methods
        internal void SetKey(Keys key, Texture2D background, Texture2D pressedBackground, Texture2D vLine, Texture2D hLine)
        {
            _vLine = vLine;
            _hLine = hLine;
            _background = background;
            _pressedBackground = pressedBackground;
            _key = key;
            switch (key)
            {
                case Keys.LeftShift:
                    _keyText = "A";
                    break;
                case Keys.Apps:
                    _keyText = "'";
                    break;
                case Keys.OemPeriod:
                    _keyText = ".";
                    break;
                case Keys.Home:
                    _keyText = "123";
                    break;
                case Keys.Help:
                    _keyText = "abc";
                    break;
                case Keys.Space:
                    _keyText = "Space";
                    break;
                case Keys.Escape:
                case Keys.Enter:
                    _keyText = "";
                    break;
                case Keys.NumPad0:
                case Keys.NumPad1:
                case Keys.NumPad2:
                case Keys.NumPad3:
                case Keys.NumPad4:
                case Keys.NumPad5:
                case Keys.NumPad6:
                case Keys.NumPad7:
                case Keys.NumPad8:
                case Keys.NumPad9:
                    _keyText = key.ToString().Replace("NumPad", string.Empty);
                    break;
                case Keys.D1:
                    _keyText = "!";
                    break;
                case Keys.D2:
                    _keyText = "@";
                    break;
                case Keys.D3:
                    _keyText = "#";
                    break;
                case Keys.D4:
                    _keyText = "$";
                    break;
                case Keys.D5:
                    _keyText = "%";
                    break;
                case Keys.D6:
                    _keyText = "^";
                    break;
                case Keys.D7:
                    _keyText = "&";
                    break;
                case Keys.D8:
                    _keyText = "*";
                    break;
                case Keys.D9:
                    _keyText = "(";
                    break;
                case Keys.D0:
                    _keyText = ")";
                    break;
                case Keys.F1:
                    _keyText = "-";
                    break;
                case Keys.F2:
                    _keyText = "+";
                    break;
                case Keys.F3:
                    _keyText = ":";
                    break;
                case Keys.F4:
                    _keyText = ";";
                    break;
                case Keys.F5:
                    _keyText = "\"";
                    break;
                case Keys.F6:
                    _keyText = "<";
                    break;
                case Keys.F7:
                    _keyText = ">";
                    break;
                case Keys.F8:
                    _keyText = "?";
                    break;
                case Keys.F9:
                    _keyText = "/";
                    break;
                case Keys.F10:
                    _keyText = "|";
                    break;
                case Keys.OemComma:
                    _keyText = ",";
                    break;
                default:
                    _keyText = key.ToString();
                    break;
            }
        }
        #endregion
    }
}